package dataflow.core;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

import dataflowScheme.SynchroPort;

public class Trigger implements ConnectableEntity {
	
	private dataflowScheme.Trigger eTrigger;
	private Set<String> ports = new HashSet<String>();
	private Map<String, ConnectionAnchor> conns = new HashMap<String, ConnectionAnchor>();

	static Logger log = Logger.getLogger(Trigger.class);
	
	public Trigger(dataflowScheme.Trigger eTrigger) {
		this.eTrigger = eTrigger;
	}
	
	public void init(){
		for(SynchroPort p : eTrigger.getPorts()){
			ports.add(p.getName());
		}
	}
	
	public void run(){
		for(Map.Entry<String, ConnectionAnchor> entry : conns.entrySet()){
			if(!entry.getValue().pe.receiveSignal(entry.getValue().portName)){
				log.error("Can't start processing entity from port '" + entry.getKey() + "'");
			}
		}
	}
	
	@Override
	public void addConnection(String portName,
			ConnectableEntity oppossiteEntity, String oppossitePortName) {
		if(ports.contains(portName)){
			conns.put(portName, new ConnectionAnchor(oppossiteEntity, oppossitePortName));
		}
		
	}

	@Override
	public boolean receiveData(Object data, String portName) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void receiveResponse(Event event, String portName) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean receiveSignal(String portName) {
		// TODO Auto-generated method stub
		return false;
	}
}
